package com.file.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.entity.Collector;
import com.file.entity.FileCollector;
import com.file.service.CollectorService;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.JsonSysdic;

/**
 * @Title: FileController.java
 * @Package com.controller
 * @author 孙梦楠
 * @date 2017年7月20日 上午10:03:22
 * @version V1.0.0
 */

@RestController
@RequestMapping(value = "/file/collector")
public class CollectorController {

	private static final Logger log = LoggerFactory.getLogger(CollectorController.class);

	@Autowired
	private CollectorService collectorservice;

	@Autowired
	private SysdicService sysdicservice;
	
	 

	// 列表
	// @RequestMapping(value = "", method = RequestMethod.GET)
	// public JsonResult<List<FileCollector>> listcollector(
	// @CookieValue(value = "opeorgcookie", required = false) String opeorgcookie) {
	//
	// return collectorservice.listcollector(opeorgcookie);
	// }

	// 条件查询列表
	@RequestMapping(value = "", method = RequestMethod.POST)
	public JsonResult<List<FileCollector>> listCollectorByConditions(

			@RequestBody Collector collector,
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie

	) {

		String collectorcode = collector.getCollectorcode();
		String collectorassetcode = collector.getCollectorassetcode();
		String collectortype = collector.getCollectortype();
		String collectorprotocol = collector.getCollectorprotocol();
		String collectormade = collector.getCollectormade();
		String collectorstatus = collector.getCollectorstatus();

		return collectorservice.listCollectorByConditions(collectorcode, collectorassetcode, collectortype,
				collectorprotocol, collectormade, collectorstatus, opeorgcookie, opetypecookie,languagecookie);

	}

	// 字典列表
	// 采集器类别
	@RequestMapping(value = "/listsysdicCollectortype", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicCollectortype(
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = sysdicservice.gettype(opetypecookie, "2");//采集器 2
		Integer state = 211;
		String Message = "Collectortype字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message,languagecookie);
	}

	// 采集器协议
	@RequestMapping(value = "/listsysdicCollectorprotocol", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicCollectorprotocol(
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = "0010";
		Integer state = 212;
		String Message = "Collectorprotocol字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message,languagecookie);
	}

	// 制造单位
	@RequestMapping(value = "/listsysdicCollectormade", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicCollectormade(
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = "0008";
		Integer state = 213;
		String Message = "Collectormade字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message,languagecookie);
	}

	// 设备状态
	@RequestMapping(value = "/listsysdicCollectorstatus", method = RequestMethod.GET)
	public JsonResult<List<JsonSysdic>> listsysdicCollectorstatus(
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		String sysdictype = "0009";
		Integer state = 214;
		String Message = "Collectorstatus字段列表";

		return sysdicservice.listsysdic(sysdictype, state, Message,languagecookie);
	}

	// 增加
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResult<Collector> addcollector(@RequestBody Collector collector,
			@CookieValue(name = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie) {

		String collectorcode = collector.getCollectorcode();
		String collectorassetcode = collector.getCollectorassetcode();
		String collectortype = collector.getCollectortype();
		String collectorprotocol = collector.getCollectorprotocol();
		String collectormade = collector.getCollectormade();
		String collectorstatus = collector.getCollectorstatus();
		String collectoraddress = collector.getCollectoraddress();

		log.info(collectorcode);
		log.info(collectorassetcode);
		log.info(collectortype);
		log.info(collectorprotocol);
		log.info(collectormade);
		log.info(opeorgcookie);
		log.info(collectorstatus);

		return collectorservice.addcollector(collectorcode, collectorassetcode, collectortype, collectoraddress,collectorprotocol,
				collectormade, opeorgcookie,opetypecookie);

	}

	// 删除
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public JsonResult<Collector> deletecollector(@RequestBody Collector collector) {
		Integer collectorid = collector.getCollectorid();

		return collectorservice.deletecollector(collectorid);

	}

	// 强制删除
	@RequestMapping(value = "/force_delete", method = RequestMethod.POST)
	public JsonResult<Collector> force_deletecollector(@RequestBody Collector collector) {
		Integer collectorid = collector.getCollectorid();

		return collectorservice.forcedeletecollector(collectorid);

	}

	// 修改页面
	@RequestMapping(value = "/getbyid", method = RequestMethod.POST)
	public JsonResult<FileCollector> updatecollector(@RequestBody Collector collector,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {
		Integer collectorid = collector.getCollectorid();

		return collectorservice.updateviewcollector(collectorid,languagecookie);

	}

	// 修改
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResult<Collector> updatecollector(@RequestBody Collector collector,
			@CookieValue(name = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie
) {

		Integer collectorid = collector.getCollectorid();
		String collectorcode = collector.getCollectorcode();
		String collectorassetcode = collector.getCollectorassetcode();
		String collectortype = collector.getCollectortype();
		String collectorprotocol = collector.getCollectorprotocol();
		String collectormade = collector.getCollectormade();
		String collectoraddress = collector.getCollectoraddress();
		
		String collectorstatus = null;

		return collectorservice.updatecollector(collectorid, collectorcode, collectorassetcode, collectortype,
				collectoraddress,collectorprotocol, collectormade, opeorgcookie, collectorstatus,opetypecookie);

	}

}
